/*
 *  Copyright 2019-2020 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.fzhucc.modules.expert.controller;

import com.fzhucc.annotation.Log;
import com.fzhucc.annotation.rest.AnonymousGetMapping;
import com.fzhucc.annotation.rest.AnonymousPostMapping;
import com.fzhucc.modules.expert.domain.dto.*;
import com.fzhucc.modules.expert.service.ExpertService;
import com.fzhucc.modules.security.domain.dto.AuthDto;
import com.fzhucc.modules.security.security.TokenProvider;
import com.fzhucc.modules.security.service.AuthenticationService;
import com.fzhucc.modules.security.service.OnlineUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author yyg
 * @date 2022-11-15
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "专家库:管理")
@RequestMapping("/api/expert")
public class ExpertController {

    private final ExpertService expertService;

    private final AuthenticationService authenticationService;

    private final TokenProvider tokenProvider;

    private final OnlineUserService onlineUserService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @AnonymousGetMapping("/download")
    /*@GetMapping(value = "/download")*/
    /*@PreAuthorize("@el.check('expert:list')")*/
    public void exportExpert(HttpServletResponse response, ExpertQueryCriteria criteria) throws IOException {
        expertService.download(expertService.queryAll(criteria), response);
    }


    /*@Log("查询专家库")
    @ApiOperation("查询科创大赛系统专家库")
    @AnonymousGetMapping("/show")
    public ResponseEntity<Object> queryExpert(ExpertQueryCriteria criteria, Pageable pageable) {
        criteria.setIsDelete(0);
        return new ResponseEntity<>(expertService.queryAll(criteria, pageable), HttpStatus.OK);
    }*/

    /*@Log("查询科创专家库及福州大赛系统专家库联合")
    @ApiOperation("查询科创专家库及福州大赛系统专家库联合")
    @AnonymousGetMapping("/showWithRace")
    public ResponseEntity<Object> queryExpertWithRace(ExpertQueryCriteria criteria, Pageable pageable) {
        criteria.setIsDelete(0);
        return new ResponseEntity<>(expertService.queryExpertWithRace(criteria, pageable), HttpStatus.OK);
    }*/

    @AnonymousPostMapping("/getFzSciExpert")
    @Log("拉取福州大赛下发的专家数据 大赛 -发 - 科创")  //大赛 -发 - 科创
    @ApiOperation("拉取福州大赛下发的专家数据 大赛 -发 - 科创")
    public ResponseEntity<Object> getFzSciExpert() {
        return new ResponseEntity<>(expertService.getFzSciExpert(), HttpStatus.OK);
    }

    @PostMapping("/distributeExpert")
    @Log("下发科创数据给大赛系统 科创 -下发- 大赛") //科创 -下发- 大赛
    @ApiOperation("下发科创数据给大赛系统 科创 -下发- 大赛")
    public ResponseEntity<Object> distributeExpert(@Validated @RequestBody ExpertDtoList expertDtoList) {
        return new ResponseEntity<>(expertService.distributeExpert(expertDtoList), HttpStatus.OK);
    }

    @PostMapping("/thawDistributeExpert")
    @Log("解冻大赛系统下发导师 科创 -下发解冻- 大赛")  //科创 -下发解冻- 大赛
    @ApiOperation("解冻大赛系统导师 科创 -下发解冻- 大赛")
    public ResponseEntity<Object> thawDistributeExpert(@Validated @RequestBody ExpertDtoList expertDtoList) {
        return new ResponseEntity<>(expertService.thawDistributeExpert(expertDtoList), HttpStatus.OK);
    }

    @PostMapping("/freezeDistributeExpert")
    @Log("冻结大赛系统下发导师 科创 -下发冻结- 大赛")  //科创 -下发冻结- 大赛
    @ApiOperation("冻结大赛系统下发导师 科创 -下发冻结- 大赛")
    public ResponseEntity<Object> freezeDistributeExpert(@Validated @RequestBody ExpertDtoList expertDtoList) {
        return new ResponseEntity<>(expertService.freezeDistributeExpert(expertDtoList), HttpStatus.OK);
    }

    @Log("查询下发数据为冻结的列表")
    @ApiOperation("查询下发数据为冻结的列表")
    @AnonymousGetMapping("/showFreeze")
    public ResponseEntity<Object> queryExpertShowFreeze(ExpertQueryCriteria criteria, Pageable pageable) {
        criteria.setIsDelete(0);
        criteria.setSciStatus(2);//冻结
        return new ResponseEntity<>(expertService.queryExpertShowFreeze(criteria, pageable), HttpStatus.OK);
    }

    @Log("查询下发数据列表")
    @ApiOperation("查询下发数据列表")
    @AnonymousGetMapping("/showDistribute")
    public ResponseEntity<Object> queryExpertDistribute(ExpertQueryCriteria criteria, Pageable pageable) {
        return new ResponseEntity<>(expertService.queryExpertDistribute(criteria, pageable), HttpStatus.OK);
    }

    /*@PostMapping("/editDistributeExpert")
    @Log("编辑大赛系统下发的导师") //科创 -下发编辑- 大赛
    @ApiOperation("编辑大赛系统下发的导师")
    public ResponseEntity<Object> editDistributeExpert(@Validated @RequestBody ExpertDtoList expertDtoList) {
        return new ResponseEntity<>(expertService.editDistributeExpert(expertDtoList), HttpStatus.OK);
    }*/

    @ApiOperation("查询科创大赛系统专家库 原有不是下发数据的专家库  即科创数据库")
    @AnonymousGetMapping("/showExperts")
    public ResponseEntity<Object> queryExpert(ExpertQueryCriteria criteria, Pageable pageable) {
        criteria.setIsDelete(0);
        criteria.setIsPull(0);
        return new ResponseEntity<>(expertService.queryAll(criteria, pageable), HttpStatus.OK);
    }

    @Log("查询科创专家库及福州大赛系统专家库联合  未入库")
    @ApiOperation("查询科创专家库及福州大赛系统专家库联合 未入库")
    @AnonymousGetMapping("/showWithOutPull")
    public ResponseEntity<Object> queryExpertWithRaceWithOutPull(ExpertQueryCriteria criteria, Pageable pageable) {
        criteria.setIsDelete(0);
        return new ResponseEntity<>(expertService.queryExpertWithRaceWithOutPull(criteria, pageable), HttpStatus.OK);
    }

    @Log("查询科创专家库及福州大赛系统专家库联合")
    @ApiOperation("查询科创专家库及福州大赛系统专家库联合")
    @AnonymousGetMapping("/show")
    public ResponseEntity<Object> queryExpertWithRaceAndIn(ExpertQueryCriteria criteria, Pageable pageable) {
        criteria.setIsDelete(0);
        return new ResponseEntity<>(expertService.queryExpertWithRaceAndIn(criteria, pageable), HttpStatus.OK);
    }

    @Log("小程序查询专家库")
    @ApiOperation("小程序查询专家库")
    @AnonymousGetMapping("/wxShow")
    public ResponseEntity<Object> wxShow(ExpertQueryCriteria criteria, Pageable pageable) {
        criteria.setIsDelete(0);
        criteria.setIsShow(1);
        return new ResponseEntity<>(expertService.queryAll(criteria, pageable), HttpStatus.OK);
    }

    @PostMapping
    @Log("新增专家库")
    @ApiOperation("新增科创大赛系统专家库")
    @PreAuthorize("@el.check('expert:add')")
    public ResponseEntity<Object> createExpert(@Validated @RequestBody ExpertAddDto resources) {
        return new ResponseEntity<>(expertService.create(resources), HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改专家库")
    @ApiOperation("修改科创大赛系统专家库")
    @PreAuthorize("@el.check('expert:edit')")
    public ResponseEntity<Object> updateExpert(@Validated @RequestBody ExpertEditDto resources) {
        expertService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除专家库")
    @ApiOperation("删除科创大赛系统专家库")
    @PreAuthorize("@el.check('expert:del')")
    public ResponseEntity<Object> deleteExpert(@RequestBody Integer[] ids) {
        expertService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @ApiOperation("上传专家头像")
    @PostMapping(value = "/uploadPhoto")
    public ResponseEntity<Object> uploadPhoto(@RequestParam MultipartFile photo, @RequestParam Integer id) {
        return new ResponseEntity<>(expertService.douploadpic(photo, id), HttpStatus.OK);
    }

    @ApiOperation("生成专家账号绑定小程序码")
    @Log("生成专家账号绑定小程序码")
    @PostMapping(value = "/generateQrCode")
    @PreAuthorize("@el.check('expert:edit')")
    public ExpertBindDto generateQrCode(@RequestParam Integer expertId) {
        return expertService.generateQrCode(expertId);
    }

    @ApiOperation("通过邀请码查询专家")
    @Log("通过邀请码查询专家")
    @AnonymousPostMapping(value = "/byCode")
    public ExpertDto findByCode(@RequestParam String code) {
        return expertService.findExpertByCode(code);
    }

    @ApiOperation("二维码绑定专家")
    @Log("二维码绑定专家")
    @AnonymousPostMapping(value = "/bind")
    public AuthDto bind(@RequestBody ExpertBindDto2 dto, HttpServletRequest request) {
        return expertService.bindExpert(dto, request);
    }

    //TODO 地区区域分类待完善
    @Log("查询专家库首页类型数量")
    @ApiOperation("查询专家库首页类型数量")
    @AnonymousPostMapping("/getCount")
    public ResponseEntity<Object> getCount(ExpertCountQueryCriteria criteria) {
        criteria.setIsDelete(0);
        return new ResponseEntity<>(expertService.findByType(criteria), HttpStatus.OK);
    }

    //TODO 地区区域分类待完善
    @Log("查询专家库分类类型")
    @ApiOperation("查询专家库分类类型")
    @AnonymousGetMapping("/getType")
    public ResponseEntity<Object> getType() {
        return new ResponseEntity<>(expertService.findAllType(), HttpStatus.OK);
    }

    @Log("专家分配账号")
    @ApiOperation("专家分配账号")
    @GetMapping(value = "/distributionUser")
    public ResponseEntity<Object> distributionUser(@RequestParam Long userId, @RequestParam int expertId) {
        expertService.distributionUser(userId, expertId);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @Log("根据专家ListId 查询对应专家信息")
    @ApiOperation("根据专家ListId 查询对应专家信息")
    @PostMapping(value = "/getExpertInfoByListId")
    public ResponseEntity<Object> getExpertInfoByListId(@RequestBody Integer[] expertIds) {
        return new ResponseEntity<>(expertService.getExpertInfoByListId(expertIds),HttpStatus.OK);
    }
}